#!/usr/bin/env bash


# 警告数
warnings_count=0
# 错误数
errors_count=0

# Colors
PURPLE='\033[35m'
RED='\033[31m'
RED_BOLD='\033[1;31m'
YELLOW='\033[33m'
YELLOW_BOLD='\033[1;33m'
GREEN='\033[32m'
GREEN_BOLD='\033[1;32m'
BLUE='\033[34m'
BLUE_BOLD='\033[1;34m'
COLOR_END='\033[0m'

echo -e "\n${PURPLE}CodeSniffer check result:${COLOR_END}"
# 被检查了的文件数
checked_file_count=0
# 找出哪些文件被更新了
for line in $(git diff  --name-status   HEAD^1  | grep '\.php$' | awk '{print $1$2}')
do
    # 文件状态
    # D: deleted
    # A: added
    # M: modified
    status=$(echo $line | grep -o '^.')

    # 不检查被删除的文件
    if [[ $status == 'D' ]]; then
        continue
    fi

    # 文件名
    file=$(echo $line | sed 's/^.//')


    output=$(phpcs --standard=OSR0 --colors --encoding=utf-8 -n -p  $file)

    warning=$(echo $output | grep -oP '([0-9]+) WARNING' | grep -oP '[0-9]+')
    error=$(echo $output | grep -oP '([0-9]+) ERROR' | grep -oP '[0-9]+')

    if [[ $warning || $error ]]; then

        msg="${file}: "

        if [[ $warning > 0 ]]; then
            msg="$msg${YELLOW_BOLD}${warning}${COLOR_END} ${YELLOW}warnings${COLOR_END} "

            let "warnings_count = warnings_count + 1"
        fi
        if [[ $error > 0 ]]; then
            msg="$msg${RED_BOLD}${error}${COLOR_END} ${RED}errors${COLOR_END}"

            let "errors_count = errors_count + 1"
        fi

        echo -e "    $msg"
        echo $output
    fi

    let "checked_file_count = checked_file_count + 1";

done

if [[ $checked_file_count == 0 ]]; then
    echo -e "    ${BLUE_BOLD}No file was checked.${COLOR_END}"
elif [[ $warnings_count == 0 && $errors_count == 0 ]]; then
    echo -e "${GREEN_BOLD}$(cowsay 'Congratulations!!!')${COLOR_END}"
elif [[ $errors_count  == 0 ]]; then
    echo -e "    ${BLUE}Good job, no errors were found!!!${COLOR_END}"
fi



if [[ $warnings_count > 0 || $errors_count > 0 ]]; then
    exit 1
fi
